//
//  PurchaseInfoCell.m
//  CandyCrushClone
//
//  Created by Petar on 5/8/15.
//  Copyright (c) 2015 Nestorov Engineering. All rights reserved.
//

#import "PurchaseInfoCell.h"
#import "Globals.h"
@import StoreKit;

@interface PurchaseInfoCell ()

@property (weak, nonatomic) IBOutlet UIImageView *purchaseImageView;
@property (weak, nonatomic) IBOutlet UILabel *descriptionLabel;
@property (weak, nonatomic) IBOutlet UIButton *buyButton;
@property (weak, nonatomic) IBOutlet UIImageView *cellBackgroundImageView;
@property (weak, nonatomic) IBOutlet UIImageView *purchaseBandImageView;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UIImageView *pricePadImageView;

@end

@implementation PurchaseInfoCell
{
    NSIndexPath *_indexPath;
    IAPProduct *_iapProduct;
    BOOL _isEnergyPurchase;
}

- (void)awakeFromNib
{
    // Initialization code
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)configureWithIAPProduct:(IAPProduct *)iapProduct forIndexPath:(NSIndexPath *)indexPath
{
    
    _iapProduct = iapProduct;
    _indexPath = indexPath;
    _isEnergyPurchase = iapProduct.isEnergyPurchase;
    
    SKProduct *skProduct = iapProduct.skProduct;
    
    // format skProduct price to local price
    NSNumberFormatter *priceFormatter = [[NSNumberFormatter alloc] init];
    [priceFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [priceFormatter setLocale:skProduct.priceLocale];
    
    [_priceLabel setText:[priceFormatter stringFromNumber:skProduct.price]];
    
    [self setPurchaseDescrition];
    [self setImages];
    
}


- (void)setPurchaseDescrition
{
    
    if ( _isEnergyPurchase == YES ) {
        
        _descriptionLabel.text = @"Unlimited";
        
    } else {
        
        // format long numbers to be separated by comma => ,
        NSNumberFormatter *rewardFormatter = [[NSNumberFormatter alloc] init];
        [rewardFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
        [rewardFormatter setGroupingSeparator: [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator]];
        
        NSString *purchaseReward = [rewardFormatter stringFromNumber:@(_iapProduct.reward)];
        
        _descriptionLabel.text = purchaseReward;
        
    }
    
}


- (void)setImages
{
    
    // Hide purchaseBand
    [_purchaseBandImageView setHidden:YES];
    
    // Set images for energy purchase
    if ( _isEnergyPurchase == YES ) {
        
        [_cellBackgroundImageView setImage:[UIImage imageNamed:@"energyCellBackground"]];
        [_pricePadImageView setImage:[UIImage imageNamed:@"energyPriceButton"]];
        [_purchaseImageView setImage:[UIImage imageNamed:@"purchaseEnergy"]];
        
    } else {
        
        // Set images for money purchase
        [_cellBackgroundImageView setImage:[UIImage imageNamed:@"purchaseCellBackground"]];
        [_pricePadImageView setImage:[UIImage imageNamed:@"purchasePriceButton"]];
        
        NSString *purchaseImageName;
        if ( [_iapProduct.productIdentifier isEqualToString:OFFER_2] ) {
            
            purchaseImageName = @"purchaseMoney_3";
            
        } else if ( [_iapProduct.productIdentifier isEqualToString:OFFER_3] ) {
            
            purchaseImageName = @"purchaseMoney_4";
            [_purchaseBandImageView setImage:[UIImage imageNamed:@"storePopular"]];
            [_purchaseBandImageView setHidden:NO];
            
        } else if ( [_iapProduct.productIdentifier isEqualToString:OFFER_4] ) {
            
            purchaseImageName = @"purchaseMoney_5";
            [_purchaseBandImageView setImage:[UIImage imageNamed:@"storeBestValue"]];
            [_purchaseBandImageView setHidden:NO];
            
        }
//        else if ( [_iapProduct.productIdentifier isEqualToString:OFFER_5] ) {
//            
//            purchaseImageName = @"purchaseMoney_4";
//            
//        } else if ( [_iapProduct.productIdentifier isEqualToString:OFFER_6] ) {
//            
//            purchaseImageName = @"purchaseMoney_5";
//            [_purchaseBandImageView setImage:[UIImage imageNamed:@"storeBestValue"]];
//            [_purchaseBandImageView setHidden:NO];
//            
//        }
        
        [_purchaseImageView setImage:[UIImage imageNamed:purchaseImageName]];
        
    }
    
}


- (IBAction)didPressBuyButton:(id)sender
{
    
    if ( self.delegate && [self.delegate respondsToSelector:@selector(didPressBuyIAPProductAtIndexPath:)] ) {
        [self.delegate didPressBuyIAPProductAtIndexPath:_indexPath];
    }
    
}


@end
